/**
	Pulsar engine. Core.
	Base application class

	Copyright (C) 2013-2014 Dmitry Dolgov

	This program is free software; you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details.
*/

#include <pulsar/pulsar.h>
#include <pulsar/src/pulsar_intr.h>

namespace pl
{

AppBase* AppBase::appBase = NULL;

#if defined(PL_ENTRY_POINT_INTERNAL)
	// No extra declarations required - user must specify Application class at own side
#elif defined(PL_ENTRY_POINT_EXTERNAL)
	// Standard application class will be declared here
	class AppLib : public App
	{
	};

	static AppLib _appLib;
#else
#error "Bad entry point settings"
#endif


} // namespace pl

/*
	Application Base ctor
*/
pl::AppBase::AppBase() :
_pCmdLine(NULL)
{
	if (appBase != NULL)
	{
		//
		// Onle one copy of Application class is allowed in system
		//
		PL_BREAK();
	}
	appBase = this;
	return;
}

/*
	Application Base dtor
*/
pl::AppBase::~AppBase()
{
	if (appBase != this)
	{
		//
		// Onle one copy of Application class is allowed in system
		//
		PL_BREAK();
	}
	appBase = NULL;
	return;
}

/*
	Initialize core (stage1)
	Internal function - called automatically
*/
void pl::AppBase::_initCore1()
{
#if defined(PL_ENTRY_POINT_INTERNAL)
	// Call user-defined code to setup basic paramenets
	pl::StartupFunc info;
	preInit(info);
#elif defined(PL_ENTRY_POINT_EXTERNAL)
	// No additional operations required
#else
#error "Bad entry point settings"
#endif

	initMemoryManager();
	return;
}

/*
	Shutdown core (stage1)
	Internal function - called automatically
*/
void pl::AppBase::_termCore1()
{
	termMemoryManager();
	return;
}

/*
	Initialize core (stage2)
	Internal function - called automatically
*/
void pl::AppBase::_initCore2(pl::CommandLine* pCmdLine)
{
	_pCmdLine = pCmdLine;

	pl::FileManager::_init();
	return;
}

/*
	Shutdown core (stage2)
	Internal function - called automatically
*/
void pl::AppBase::_termCore2()
{
	pl::FileManager::_term();

	PL_DELETE _pCmdLine;
	_pCmdLine = NULL;
	return;
}

//
// End of file 'pulsar_appbase.cpp'
//
